﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ZLCMS.Business.Util;
using ZLCMS.Data.Model;
using DotNet.Common.NHibernateUtil.QueryFilter;
using NHibernate;

namespace ZLCMS.Web.SystemManage.Basics.Shop.Member
{
    public partial class SelectMemberDialog : System.Web.UI.Page
    {

        protected ZLCMS.Business.DwzFrame.FrameHelper _framePage;

        protected void Page_Load(object sender, EventArgs e)
        {
            _framePage = new ZLCMS.Business.DwzFrame.FrameHelper(this);

            //设置分页大小
            int pageSize = _framePage.IsAjaxPagerPostBack ? _framePage.PageSize : 20;

            //设置页码
            int pageIndex = _framePage.IsAjaxPagerPostBack ? _framePage.PageIndex : 1;

            string sql = " from MemberInfo where OwnerAccount=:acc ";

            QueryFilterList filters = new QueryFilterList();
            if (!string.IsNullOrEmpty(Request["account"]))
            {
                filters.Add(new StringFilter("AccountId", "%" + Request["account"] + "%", StringCompairType.Like));
            }
            if (!string.IsNullOrEmpty(Request["name"]))
            {
                filters.Add(new StringFilter("MemberName", "%" + Request["name"] + "%", StringCompairType.Like));
            }
            if (!string.IsNullOrEmpty(Request["phone"]))
            {
                filters.Add(new StringFilter("Telephone", "%" + Request["phone"] + "%", StringCompairType.Like));
            }
            foreach (DataFilter filter in filters)
            {
                sql += " and " + filter.GetSection();
            }

            using (NHibernate.ISession session = DotNet.Common.NHibernateUtil.SessionFactoryHelper.CreateSession())
            {
                //判断是否导出EXCEL操作
                //if (!string.IsNullOrEmpty(Request["exportexcel"]))
                //{
                //    IQuery query = session.CreateQuery(sql)
                //       .SetParameter("acc", UserKit.GetUserAccountFailLogin());

                //    filters.SetUp(query);

                //    IList<MemberInfo> list = query.List<MemberInfo>();


                //    StringWriter sw = new StringWriter();
                //    sw.WriteLine("帐号\t姓名\t邮箱\t手机\t积分\t等级\t备注");
                //    foreach (MemberInfo item in list)
                //    {
                //        sw.WriteLine("'" + item.AccountId + "\t" + item.MemberName + "\t" + item.Email + "\t'"
                //             + item.Telephone + "\t" + item.Score + "\t" + item.Level + "\t" + item.Remarks + "\t");
                //    }
                //    sw.Close();

                //    OutputKit.OutputExcel(Response, sw);
                //}
                //else
                {

                    IQuery query = session.CreateQuery("select count(*)" + sql)
                        .SetParameter("acc", UserKit.GetUserAccountFailLogin());
                    filters.SetUp(query);

                    //设置记录总数
                    _framePage.RecordCount = Convert.ToInt32(query.UniqueResult());

                    query = session.CreateQuery(sql)
                        .SetParameter("acc", UserKit.GetUserAccountFailLogin())
                        .SetFirstResult((pageIndex - 1) * pageSize)
                        .SetMaxResults(pageSize);
                    filters.SetUp(query);

                    //设置数据源
                    repeater1.DataSource = query.List<MemberInfo>();


                }
            }

            repeater1.DataBind();
        }
    }
}